package com.zhongnan.demo6.shengchanxiaofei;

public class Foodie extends Thread{
    private Desk t;

    public Foodie(Desk t) {
        this.t = t;
    }

    @Override
    public void run() {
        /*消费者步骤
        判断桌子上是否有食物
        没有就等待
        有就开吃
        吃完就叫醒等待的厨师继续生产
        汉堡的数量减一
         */
        while(true) {
            synchronized (t.getLock()){
                if (t.getCount() == 0) {
                    break;
                } else {
                    if (t.isFlag()) {
                        System.out.println("消费者正在吃汉堡");
                        t.setFlag(false);
                        t.getLock().notifyAll();
                        t.setCount(t.getCount() - 1);
                    } else {
                        try {
                            t.getLock().wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}
